package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.service.SubmitRequestServicePrx;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import com.plusmoney.util.ParameterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 提交审批时,预先查看抄送人
 *
 * @author
 */
@WebServlet("/getRequestCc")
public class GetRequestCc extends AbstractServlet {

    private static final Logger logger = LoggerFactory.getLogger(GetRelateHistory.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);
            Query query = new Query();
            query.setCompanyId(user.getCompanyId());
            query.setTemplateId(ParameterUtils.getIntegerParameter(req, "templateId"));

            SubmitRequestServicePrx submitRequestService = ServiceUtils.getSubmitRequestServicePrx();
            List<Integer> approvalCcList = submitRequestService.getApprovalCc(user, query);

            returnResp(resp, approvalCcList);
        } catch (Throwable throwable) {
            onThrowable(req, resp, throwable);
        }
    }

    private void returnResp(HttpServletResponse resp, List list) throws IOException {
        JSONObject json = new JSONObject();
        json.put("erpCode", "000");
        json.put("erpMsg", "成功");
        json.put("erpData", list);

        logger.info(json.toJSONString());

        printJson(resp, json.toJSONString());
    }

}
